import { getUserCenter } from "@/api/user";
import { TOKEN_KEY } from "@/enums/cacheEnums";
import cache from "@/utils/cache";
import { defineStore } from "pinia";
import { loginApi } from "@/api/account";

interface UserSate {
  userInfo: Record<string, any>;
  token: string | null;
  temToken: string | null;
}
export const useUserStore = defineStore("user", {
  state: (): UserSate => ({
    userInfo: {},
    token: cache.get(TOKEN_KEY) || null,
    temToken: null,
  }),
  getters: {
    isLogin: (state) => !!state.token,
  },
  actions: {
    async getUser() {
      const data = await getUserCenter({
        token: this.token || this.temToken,
      });
      this.userInfo = data;
    },
    async login(data: any) {
      const {userId,token} = await loginApi({ ...data });
      this.token = token;
      cache.set(TOKEN_KEY, token);
    },
    logout() {
      this.token = "";
      this.userInfo = {};
      cache.remove(TOKEN_KEY);
    },
  },
});
